package com.stockz.ui.swing.charting.painter.indicator;

import java.awt.Color;
import java.awt.Graphics2D;

import com.stockz.ui.swing.charting.display.indicator.AbstractDisplayableIndicatorConfiguration;
import com.stockz.ui.swing.charting.util.MathUtil;
import com.stockz.ui.swing.charting.view.StockChartPanelState;
import com.stockz.ui.swing.charting.view.StockRectangle;

public class MACDIndicatorPainter {

	public static void paint(Graphics2D g, AbstractDisplayableIndicatorConfiguration indicatorDisplay, double[] values) {
		StockRectangle rectangle = indicatorDisplay.getRectangle();
		if (rectangle != null && !rectangle.isEmpty()) {
			g.setClip(rectangle);
			g.setColor(indicatorDisplay.getColor());

			double min = rectangle.getMinimumValueVisible();
			double max = rectangle.getMaximumValueVisible();
			
			StockChartPanelState state = indicatorDisplay.getStockChartPanel().getStockChartPanelState();
			for (int i = state.getEndIndex() - 1; i >= state.getStartIndex(); i--) {
				if (i >= values.length 
						|| i <= 0
						|| values[i] > indicatorDisplay.getRectangle().getMaximumValueVisible()
						|| values[i] < indicatorDisplay.getRectangle().getMinimumValueVisible()) {
					continue;
				}
				
				int x = MathUtil.fitX(rectangle.x,state.getTimeUnitSpace(),state.getEndIndex(),i);
				int y = MathUtil.fitY(rectangle.y,rectangle.y + rectangle.height,min,max,values[i],state.isSemiLog());
				int x1 = MathUtil.fitX(rectangle.x,state.getTimeUnitSpace(),state.getEndIndex(),i - 1);
				int y1 = MathUtil.fitY(rectangle.y,rectangle.y + rectangle.height,min,max,values[i - 1],state.isSemiLog());
				int x0 = x1;
				int y0 = MathUtil.fitY(rectangle.y,rectangle.y + rectangle.height,min,max,0,state.isSemiLog());
				if(values[i] > 0){
					g.setColor(Color.green);
				}else{
					g.setColor(Color.red);
				}
				
				g.fillPolygon(new int[]{x0,x,x,x1}, new int[]{y0,y0,y,y1}, 4);
				
			}
		}
	}
	
}
